package com.example.demo111111.service.impl;

import com.example.demo111111.common.JwtTokenUtils;
import com.example.demo111111.dao.UserDao;
import com.example.demo111111.entity.User;
import com.example.demo111111.service.UserService;
import com.example.demo111111.util.EmailValidator;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Resource
    private UserDao userDao;


//    @Override
//    public User comparePassword(User user){
//        User nowUser = userDao.findUserByAccount(user.getAccount());
//        if (nowUser==null){
//
//            throw new RuntimeException("用户不存在");
//        }
//
//        String password = user.getPassword();
//        String nowPassword = nowUser.getPassword();
//
//        if (password.equals(nowPassword)){
//            String token= JwtTokenUtils.genToken(nowUser.getAccount(),nowUser.getPassword());
//            nowUser.setToken(token);
//            return nowUser;
//        }else {
//            throw new RuntimeException("用户密码错误");
//        }
//
//
//    }
@Override
public User comparePassword(User user) {
    try {
        // 1. 查询用户
        User nowUser = userDao.findUserByAccount(user.getAccount());
        if (nowUser == null) {
            throw new RuntimeException("用户不存在");
        }

        // 2. 比较密码
        String inputPassword = user.getPassword();
        String storedPassword = nowUser.getPassword();

        if (!inputPassword.equals(storedPassword)) {
            throw new RuntimeException("用户密码错误");
        }

        // 3. 生成 Token 并返回用户信息
        String token = JwtTokenUtils.genToken(nowUser.getAccount(), nowUser.getPassword());
        nowUser.setToken(token);
        return nowUser;

    } catch (Exception e) {
        // 捕获可能的异常（如数据库访问异常、JWT 生成异常等）
        throw new RuntimeException("登录验证失败: " + e.getMessage(), e);
    }
}

    @Override
    public User findUserByAccount(String account) {
        User user = userDao.findUserByAccount(account);
        return user;
    }



    @Override
    public Boolean registertUser(User user) {
        user.setCreate_time(LocalDateTime.now());
        user.setUpdate_time(LocalDateTime.now());
        if (user.getImage()==null ){
            user.setImage("https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg");
        }
        if (user.getName()==null ){
            long timestamp = System.currentTimeMillis();
            String randomStr = RandomStringUtils.randomAlphabetic(4).toLowerCase(); // 4位随机字母
            user.setName("user_" + timestamp + "_" + randomStr);

        }
        if (user.getPassword()=="" ||user.getPassword()==null){
            throw new RuntimeException("密码不能为空");
        }

        User userByAccount = userDao.findUserByAccount(user.getAccount());

        if (userByAccount==null){
            Boolean result = userDao.insertUser(user);
            return result;
        }else {
            throw new RuntimeException("账号已经存在");
        }

    }


    @Override
    public Boolean delete(User user ) {
        Boolean result=userDao.delete(user);

        return result;
    }

//    @Override
//    public Boolean updatePassword(ModifyUser modifyUser) {
//        String oldPassword=modifyUser.getOldPassword();
//
//        User newUser=userDao.findUserByAccount(modifyUser.getAccount());
//        if (newUser==null){
//            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "用户不存在");
//        }
//        if (!oldPassword.equals(newUser.getPassword())) {
//            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "旧密码和新密码不能相等");
//
//        }
//        User user=new User();
//        user.setAccount(modifyUser.getAccount());
//        user.setPassword(modifyUser.getNewPassword());
//        Boolean result=userDao.updateUser(user);
//
//        return result;
//    }

    @Override
    public List<User> findAlluser(User user) {
        int pageIndex = user.getPage();
        int pageSize = user.getPageSize();
        pageIndex=(pageIndex-1)*pageSize;
        user.setPage(pageIndex);
        List<User> users = userDao.selectAllUser(user);

        return users;
    }

    @Override
    public List<User> findUser(User user) {
        List<User> users= userDao.findUser(user);
        return users;
    }

    @Override
    public Boolean updateUser(User user) {


//    try {
//        if (!EmailValidator.isValidEmail(user.getEmail())){
//            throw new Exception("邮箱格式不正确");
//        }
//
//    } catch (Exception e) {
//        throw new RuntimeException(e);
//    }

        user.setUpdate_time(LocalDateTime.now());
        Boolean result=userDao.updateUser(user);
        return result;
    }
}
